package Window;

import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;

public class MainWindow {

	public JFrame frmSpacebuildergame;
	public boolean GameStartFlag=false;
	public JLabel GameState=null;
	public int thisPlayer=0;
	public JLabel GoState=null;
	public int Player1Now=0;
	public int Player2Now=0;
	public String[] levelInfo={"起点","安他提卡星球","阿米巴星球","快进四步","守护飞轮海星球","幸运箱","倒退4步","赛罕塔拉星球","终极挑战","终点"};
	public int[] player1x={829,829,829,659,499,340,186,25,25,25};
	public int[] player1y={348,192,27,27,27,27,27,27,192,348};
	public int[] player2x={829,829,829,659,499,340,186,25,25,25};
	public int[] player2y={375,219,54,54,54,54,54,54,219,375};
	public JLabel player1;
	public JLabel player2;

	public void setGameState(String s){
		GameState.setText("游戏状态："+s);
	}
	
	public void setGoState(String s){
		GoState.setText("前进："+s);
	}
	
	public int Player1MoveForward(int step){
		player1.setBounds(player1x[step], player1y[step], 61, 17);
		return 1;
	}
	
	public int Player2MoveForward(int step){
		player2.setBounds(player2x[step], player2y[step], 61, 17);
		return 1;
	}
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frmSpacebuildergame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmSpacebuildergame = new JFrame();
		frmSpacebuildergame.setTitle("SBG-SpaceBuilderGame-空间建造者游戏");
		frmSpacebuildergame.setBounds(100, 100, 950, 510);
		frmSpacebuildergame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmSpacebuildergame.getContentPane().setLayout(null);
		URL iconUrl=MainWindow.class.getResource("background.jpg");
		ImageIcon ii=new ImageIcon(iconUrl);
		
		JButton button = new JButton("查看规则");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String[] sa=new String[2];
				sa[0]="rules";
				sa[1]="SBG-SpaceBuilderGame-空间建造者游戏	规则";
				ShowPictureWindow.main(sa);
			}
		});
		
		GoState = new JLabel("前进：");
		GoState.setVisible(false);
		
		final JButton stopGame = new JButton("停止游戏");
		stopGame.setBounds(186, 209, 107, 27);
		stopGame.setVisible(false);
		
		final JButton PlayerGo = new JButton("选手1前进");
		final JLabel PlayerState = new JLabel("当前选手：");
		PlayerState.setVisible(false);
		PlayerState.setFont(new Font("Dialog", Font.BOLD, 35));
		PlayerState.setForeground(Color.RED);
		PlayerState.setBounds(428, 205, 353, 89);
		
		final JButton startGame = new JButton("开始游戏");
		startGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PlayerState.setVisible(true);
				GameStartFlag=true;
				setGameState("等待选手1前进……");
				PlayerState.setText("当前选手：选手1");
				thisPlayer=1;
				startGame.setVisible(false);
				stopGame.setVisible(true);
				GoState.setVisible(true);
				PlayerGo.setVisible(true);
				
			}
		});
		
		PlayerGo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(PlayerGo.getText().equals("选手1前进")){
					PlayerGo.setText("选手2前进");
					setGameState("选手1的回合");
					PlayerState.setText("当前选手：选手1");
					int ran=((int)(Math.random()*10%3))+1;
					setGoState(ran+"个单位");
					ran=ran+Player1Now;
					Player1Now=ran;
					if(Player1Now==3){
						JOptionPane.showMessageDialog(frmSpacebuildergame, "选手1前进四步！");
						Player1Now=7;
						ran=7;
					}
					if(Player1Now==6){
						JOptionPane.showMessageDialog(frmSpacebuildergame, "选手1倒退四步！");
						Player1Now=2;
						ran=2;
					}
					if (Player1Now==5) {
						JOptionPane.showMessageDialog(frmSpacebuildergame, "幸运箱！再来一个回合！");
						PlayerGo.setText("选手1前进");
						setGameState("还是选手1的回合");
						return ;
					}
					if(ran>=9){
						JOptionPane.showMessageDialog(frmSpacebuildergame, "游戏结束，胜出者是：选手1！");
						GameStartFlag=false;
						setGameState("未开始");
						thisPlayer=0;
						startGame.setVisible(true);
						stopGame.setVisible(false);
						GoState.setVisible(false);
						PlayerGo.setVisible(false);
						PlayerGo.setText("选手1前进");
						Player1MoveForward(0);
						Player2MoveForward(0);
						Player1Now=0;
						Player2Now=0;
						PlayerState.setVisible(false);
						return ;
					}
					Player1MoveForward(ran);
					String sa[]=new String[2];
					sa[0]=Integer.toString(ran);
					sa[1]="选手1的回合-"+levelInfo[ran];
					ShowPictureWindow.main(sa);
					PlayerState.setText("当前选手：选手2");
				}else{
					PlayerGo.setText("选手1前进");
					setGameState("选手2的回合");
					PlayerState.setText("当前选手：选手2");
					int ran=((int)(Math.random()*10%3))+1;
					setGoState(ran+"个单位");
					ran=ran+Player2Now;
					Player2Now=ran;
					if(Player2Now==3){
						JOptionPane.showMessageDialog(frmSpacebuildergame, "选手2前进四步！");
						Player2Now=7;
						ran=7;
					}
					if(Player2Now==6){
						JOptionPane.showMessageDialog(frmSpacebuildergame, "选手2后退四步！");
						Player2Now=2;
						ran=2;
					}
					if (Player2Now==5) {
						JOptionPane.showMessageDialog(frmSpacebuildergame, "幸运箱！再来一个回合！");
						PlayerGo.setText("选手2前进");
						setGameState("还是选手2的回合");
						return ;
					}
					if(ran>=9){
						JOptionPane.showMessageDialog(frmSpacebuildergame, "游戏结束，胜出者是：选手2！");
						GameStartFlag=false;
						setGameState("未开始");
						thisPlayer=0;
						startGame.setVisible(true);
						stopGame.setVisible(false);
						GoState.setVisible(false);
						PlayerGo.setVisible(false);
						PlayerGo.setText("选手1前进");
						Player1MoveForward(0);
						Player2MoveForward(0);
						Player1Now=0;
						Player2Now=0;
						PlayerState.setVisible(false);
						return ;
					}
					Player2MoveForward(ran);
					String sa[]=new String[2];
					sa[0]=Integer.toString(ran);
					sa[1]="选手2的回合-"+levelInfo[ran];
					ShowPictureWindow.main(sa);
					PlayerState.setText("当前选手：选手1");
				}
				
			}
		});
		PlayerGo.setVisible(false);
		
		JLabel lblNewLabel = new JLabel("本系统由Jstudio-张雨同开发");
		lblNewLabel.setBounds(383, 453, 203, 17);
		frmSpacebuildergame.getContentPane().add(lblNewLabel);
		
		
		frmSpacebuildergame.getContentPane().add(PlayerState);
		
		player2 = new JLabel("选手2");
//		URL player2_icon=MainWindow.class.getResource("player2.jpg");
//		ImageIcon player2_lable_image=new ImageIcon(player2_icon);
//		player2.setIcon(player2_lable_image);
		player2.setForeground(Color.YELLOW);
		player2.setFont(new Font("Dialog", Font.BOLD, 20));
		player2.setBackground(Color.WHITE);
		player2.setBounds(829, 375, 61, 17);
		frmSpacebuildergame.getContentPane().add(player2);
		
		player1 = new JLabel("选手1");
//		URL player1_icon=MainWindow.class.getResource("player1.jpg");
//		ImageIcon player1_lable_image=new ImageIcon(player1_icon);
//		player1.setIcon(player1_lable_image);
		player1.setFont(new Font("Dialog", Font.BOLD, 20));
		player1.setForeground(Color.YELLOW);
		player1.setBackground(Color.WHITE);
		player1.setBounds(829, 348, 61, 17);
		frmSpacebuildergame.getContentPane().add(player1);
		
		GoState.setBounds(311, 245, 300, 27);
		frmSpacebuildergame.getContentPane().add(GoState);
		
		PlayerGo.setBounds(186, 245, 107, 27);
		frmSpacebuildergame.getContentPane().add(PlayerGo);
		startGame.setBounds(186, 209, 107, 27);
		frmSpacebuildergame.getContentPane().add(startGame);
		frmSpacebuildergame.getContentPane().add(stopGame);
		
		stopGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PlayerState.setVisible(false);
				GameStartFlag=false;
				setGameState("未开始");
				thisPlayer=0;
				startGame.setVisible(true);
				stopGame.setVisible(false);
				GoState.setVisible(false);
				PlayerGo.setVisible(false);
				PlayerGo.setText("选手1前进");
				Player1MoveForward(0);
				Player2MoveForward(0);
			}
		});
		
		GameState = new JLabel("游戏状态：未开始");
		GameState.setBounds(311, 174, 300, 27);
		frmSpacebuildergame.getContentPane().add(GameState);
		button.setBounds(186, 174, 107, 27);
		frmSpacebuildergame.getContentPane().add(button);
		
		JLabel label = new JLabel("");
		label.setIcon(ii);
		label.setBounds(0, 0, 960, 500);
		frmSpacebuildergame.getContentPane().add(label);
	}
}
